﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Repository;
using ADShop.Models;
using System.Web.Routing;

namespace ADShop.Areas.Admin.Controllers
{
    public class MemberController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (Session["Admin"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Oops",
                    action = "Index"
                }));
            }
        }
        //private ADShopEntities db = new ADShopEntities();
        private EShopEntities1 db = new EShopEntities1();

        //
        // GET: /Admin/Member/

        public ActionResult Index()
        {
            return View(db.Member.ToList());
        }

        //
        // GET: /Admin/Member/Details/5

        public ActionResult Details(int id = 0)
        {
            Member member = db.Member.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            ViewBag.IDUser = new SelectList(db.InfoMember, "UserID", "FirstName");
            return View(member);
        }

        //
        // GET: /Admin/Member/Create

        public ActionResult Create()
        {
            ViewBag.UserID = new SelectList(db.InfoMember, "UserID", "FirstName");
            ViewBag.IDPrivilege = new SelectList(db.Privilege, "IDPrivilege", "NamePrivilege");
            return View();
        }

        //
        // POST: /Admin/Member/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Member member)
        {
            if (ModelState.IsValid)
            {
                member.Password = Global.MD5(member.Password);
                db.Member.Add(member);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.UserID = new SelectList(db.InfoMember, "IDUser", "FirstName", member.IDUser);
            return View(member);
        }

        //
        // GET: /Admin/Member/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Member member = db.Member.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            ViewBag.UserID = new SelectList(db.InfoMember, "UserID", "FirstName", member.IDUser);
            ViewBag.IDPrivilege = new SelectList(db.Privilege, "IDPrivilege", "NamePrivilege", member.IDPrivilege);
            return View(member);
        }

        //
        // POST: /Admin/Member/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Member member,FormCollection fc)
        {
            if (ModelState.IsValid)
            {
               // db.Member.SingleOrDefault(d => d.IDUser == member.IDUser).IDPrivilege = Convert.ToInt32(fc["ddlPri"]);
                db.Entry(member).State = EntityState.Modified;
                db.Member.SingleOrDefault(d => d.IDUser == member.IDUser).Password = Global.MD5(member.Password);
               
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserID = new SelectList(db.InfoMember, "UserID", "FirstName", member.IDUser);
            ViewBag.IDPrivilege = new SelectList(db.Privilege, "IDPrivilege", "NamePrivilege", member.IDPrivilege);
            return View(member);
        }

        //
        // GET: /Admin/Member/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Member member = db.Member.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        //
        // POST: /Admin/Member/Delete/5

   
        public ActionResult DeleteConfirmed(int id)
        {
            Member member = db.Member.Find(id);
            db.Member.Remove(member);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}